import useDictStore from '@/store/modules/dict'
import { dictGetListByCode } from '@/api/sys/dict'
import { getDicts } from '@/api/sys/dict/data'
import {toRefs} from "@vueuse/core";

/**
 * 获取字典数据
 */
export function useDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((dictCode, index) => {
      res.value[dictCode] = [];
      const dicts = useDictStore().getDict(dictCode);
      if (dicts) {
        res.value[dictCode] = dicts;
      } else {
        // getDicts(dictCode).then(resp => {
        dictGetListByCode({code: dictCode}).then(resp => {
          // res.value[dictCode] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
          if (resp.data) {
            res.value[dictCode] = resp.data.map(p => ({ name: p.name, code: p.code, id: p.id, parentId: p.parentId }))
            useDictStore().setDict(dictCode, res.value[dictCode]);
          }
        })
      }
    })
    return toRefs(res.value);
  })()
}